package com.example.mallapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mallapi.entity.Address;
import com.example.mallapi.entity.Cart;
import com.example.mallapi.entity.Product;
import com.example.mallapi.entity.User;
import com.example.mallapi.mapper.AddressMapper;
import com.example.mallapi.mapper.CartMapper;
import com.example.mallapi.mapper.ProductMapper;
import com.example.mallapi.mapper.UserMapper;
import com.example.mallapi.qo.UserQo;
import com.example.mallapi.service.IProductService;
import com.example.mallapi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 * @author zhengpf
 * @since 2023-06-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private IProductService productService;

    @Override
    public IPage<User> page(UserQo qo) {
        //条件分页查询
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(qo.getKeyword()),User::getUserName,qo.getKeyword())
                .or()
                .like(StringUtils.isNotBlank(qo.getKeyword()),User::getUserNickname,qo.getKeyword());

        Page<User> page = new Page(qo.getPageNum(),qo.getPageSize());
        page = baseMapper.selectPage(page,wrapper);


        //获取page对象每一个用户的详细地址详细
        List<User> users = page.getRecords();
        if(users != null && users.size() > 0){
            for (User user : users) {
                //获取所在地详细地址详细
                //获取当前记录的地址编码对应的地址对象
                Address address = addressMapper.selectById(user.getUserAddress());//当前地址的对象  东城区
//                address = addressMapper.selectById(address.getAddressRegionid());
//                address = addressMapper.selectById(address.getAddressRegionid());

                //如果当前编码与父级编码不一致，说明还有上一级(当去为子级）
                String addressNameDetail = address.getAddressName();
                while (!address.getAddressAreaid().equals(address.getAddressRegionid())){
                    address = addressMapper.selectById(address.getAddressRegionid());  //父级地址对象
                    //拼接最终地址
                    addressNameDetail = address.getAddressName() + " " + addressNameDetail;
                }

                //设置当前用户的最终地址
                address.setAddressName(addressNameDetail);
                //用户所在地
                user.setUserAddressObj(address);


                //家乡详细地址信息
                Address homeAddress = addressMapper.selectById(user.getUserHomeplace());//  查询用户家乡地址对象
                String homeAddressDetailName = homeAddress.getAddressName();
                //如果当前地址不等于 父级地址：说明还有上一层父级地址需要查询
                while (!homeAddress.getAddressAreaid().equals(homeAddress.getAddressRegionid())){
                    homeAddress = addressMapper.selectById(homeAddress.getAddressRegionid());
                    //拼接最终地址
                    homeAddressDetailName = homeAddress.getAddressName() + " " + homeAddressDetailName;
                }
                //为用户设置完整家乡地址
                homeAddress.setAddressName(homeAddressDetailName);
                //设置用户家乡地址对象，返回给前端相应
                user.setUserHomeAddressObj(homeAddress);


                //查询用户购物车信息
                LambdaQueryWrapper<Cart> cartWrapper = new LambdaQueryWrapper<>();
                cartWrapper.eq(Cart::getCartUserId,user.getUserId());
                List<Cart> carts = cartMapper.selectList(cartWrapper);


                //查询购物车的商品信息 商品名等
                if(carts != null && carts.size() > 0){
                    for (Cart cart : carts) {
                        Product product = productService.getById(cart.getCartProductId());
                        //将查询到的商品信息放入到购物车项对象中
                        cart.setProduct(product);
                    }
                }

                //将购物车信息存放到user对象中
                user.setCarts(carts);
            }
        }
        return page;
    }
}
